import qs from 'qs';
import {
    api,
    httpService,
    http
} from '@/common/http/http.js';
import navConfig from './navConfig.js';

function getCurrentMenu(_nav) {
    let navNew = [],
        navNew1 = [];
    _nav.forEach(function (item) {
        navNew.push(Object.assign({}, {
            ...item
        }));
        navNew1.push(Object.assign({}, {
            ...item
        }));
    })
    var nav = []
    var allPermissions = localStorage.getItem('allPermissions') ? JSON.parse(localStorage.getItem('allPermissions')) : 0;
    if (allPermissions == 0) {
        nav = localStorage.getItem('nav') ? JSON.parse(localStorage.getItem('nav')) : [];
    } else {
        nav = [
            '3', '50', '4', '96', '64', '84', '83', '28', '57',
            '71', '8', '9', '87', '11', '27', '97', '98', '103',
            '6', '7', '88', '30', '37', '49', '36', '79', '1009',
            '15', '16', '17', '29', '38', '55', '58',
            '18', '19', '31', '56',
            '60', '23', '53', '54', '94', '95', '61', '62', '63', '102',
            '39', '40', '80', '72', '82', '73', '99', '81', '69',
            '41', '92', '93', '100', '42', '43', '90',
            '44', '74', '75', '89',
            '51', '52',
            '70', '25', '68', '85', '86',
            '1', '2', '101', '14', '26', '33', '21', '35', '65', '78', '200'
        ]
    }
    //	var nav = [
    //			'3','50','4','96','64','84','83','28','57',
    //			'71','8','9','87','11','27','97','98','103',
    //			'6','7','88','30','37','49','36','79','1009',
    //			'15','16','17','29','38','55','58',
    //			'18','19','31','56',
    //			'60','23','53','54','94','95','61','62','63','102',
    //			'39','40','80','72','82','73','99','81','69',
    //			'41','92','93','100','42','43','90',
    //			'44','74','75','89',
    //			'51','52',
    //			'70','25','68','85','86',
    //			'1','2','101','14','26','33','21','35','65','78','200'
    //	]
    navNew1.forEach((item, index) => {
        let childArr = [];
        if (item.child.length > 0) {
            item.child.forEach((subItem) => {
                nav.forEach((nItem) => {
                    if (subItem.id == nItem) {
                        childArr.push(subItem);
                    }
                })
            })
            navNew[index].child = childArr;
        }
    })
    var parentArr = []
    for (let i = 0; i < navNew.length; i++) {
        for (let k = 0; k < nav.length; k++) {
            if (navNew[i].id == nav[k] && navNew[i].child.length > 0) {
                parentArr.push(navNew[i])
            }
        }
    }
    return parentArr;
}
export const getNavList = (params = {}) => {
    var currentMenu = getCurrentMenu(navConfig);
    var navArray = [];
    var Nav = [{
        id: 1,
        name: '概况',
        completeName: '概况',
        icon: 'icon-jianzhuanquan',
        child: [{
            id: 11,
            pid: 1,
            name: "概况",
            link: "/index/home"
        }, ]
    }, ]
    navArray = Nav;
    currentMenu.forEach(function (item, idx) {
        var _child = [];
        item.child.forEach(function (data, idx1) {
            var $obj = {
                id: Number((idx + 2) * 10 + idx1),
                pid: Number(idx + 2),
                name: data.name,
                link: data.link
            }
            _child.push($obj);
        })
        var obj = {
            id: Number(idx + 2),
            name: item.name,
            completeName: item.completeName,
            icon: item.icon,
            child: _child
        }
        navArray.push(obj);
    });
    let promise = new Promise(function (resolve, reject) {
        let data = {
            userName: '用户名',
            navList: navArray
        };
        resolve(data);
    });
    return promise;
}
